﻿using kis.api.model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace kis.api.orm
{

    /// <summary>
    /// 
    /// </summary>
    public partial class Function
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public KisUrl KisUrlAdd(KisUrl item)
        {
            try
            {
                return this.db.Insertable(item)
                    .ExecuteReturnEntity();
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(@"执行方法{0}发生错误，错误原因：{1}", System.Reflection.MethodBase.GetCurrentMethod().Name, ex.Message));
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public KisUrl KisUrlGetModel(int id)
        {
            try
            {
                var seos = this.db.Queryable<KisUrl>()
                    .Where(w => w.id == id)
                    .Single();

                return seos;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(@"执行方法{0}发生错误，错误原因：{1}", System.Reflection.MethodBase.GetCurrentMethod().Name, ex.Message));
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="item"></param>
        public void KisUrlUpdate(KisUrl item)
        {
            try
            {
                this.db.Updateable(item)
                    .Where(w => w.id == item.id)
                    .ExecuteCommand();
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(@"执行方法{0}发生错误，错误原因：{1}", System.Reflection.MethodBase.GetCurrentMethod().Name, ex.Message));
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public List<KisUrl> KisUrlGetList(string name = "", string url = "")
        {
            try
            {
                return this.db.Queryable<KisUrl>()
                    .Where(w => (w.name.Contains(name) || w.jpm.Contains(name)) &&
                                        w.url.Contains(url))
                    .OrderBy(o => o.serial, SqlSugar.OrderByType.Asc)
                    .ToList();
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(@"执行方法{0}发生错误，错误原因：{1}", System.Reflection.MethodBase.GetCurrentMethod().Name, ex.Message));
            }
        }
    }
}
